
# wordtype.py
# each word has its own configuarble fields

from PyQt4 import QtGui, QtCore

class WordType:
    def __init__(self, parent, name, fields):
        self.vocab = parent
        self.name = name
        self.fields = fields # this should be an array
    
    def deleteField(self, fieldName):
        for field in self.fields:
            if field.name == fieldName:
                self.fields.remove(field)
                break
        
    def toCSV(self, csvWriter):
        csvWriter.writerow([self.name])
        for field in self.fields:
            field.toCSV(csvWriter)
        csvWriter.writerow(['/' + self.name])
        
    def writeXML(self, xmlWriter):
        xmlWriter.writeStartElement('wordType')
        xmlWriter.writeAttribute('name', self.name)
        for field in self.fields:
            field.writeXML(xmlWriter)
        xmlWriter.writeEndElement()
        
class Field:
    def __init__(self, vocab, name, label, font, position, show):
        self.vocab = vocab
        self.name = str(name)
        self.label = str(label) # for the UI
        #self.font = QtGui.QFont() # the font to use for the value string
        #self.font.fromString(font)
        self.font = str(font) # refers to a particular font in vocab.fonts
        self.position = int(position) # where we put it on the flashcard
        self.show = int(show) # when we reveal this field
    
    def instantiate(self, value, parent):
    	assert(self.position != FieldPosition.PREFIX)
    	assert(self.position != FieldPosition.POSTFIX)
    	return FieldInstance(self, value, parent)
    
    def generateEditLayout(self, value):
        editLayout = QtGui.QHBoxLayout()
        
        editLabel = QtGui.QLabel(self.label)
    	editLabel.setAlignment(QtCore.Qt.AlignRight)
    	editLayout.addWidget(editLabel)
    	    
    	editValue = QtGui.QLineEdit(unicode(value))
        # some special cases where we need to make sure a reasonable int is entered
        if self.name == 'chapter':
            validator = QtGui.QIntValidator(None)
            validator.setBottom(0)
    	    editValue.setValidator(validator)
        elif self.name == 'difficulty':
            validator = QtGui.QDoubleValidator(None)
    	    editValue.setValidator(validator)
    	    
    	editFont = QtGui.QFont(self.vocab.fonts[self.font])
    	editFont.setPointSize(12)
    	editValue.setFont(editFont)
    	editValue.setAlignment(QtCore.Qt.AlignLeft)
    	editLayout.addWidget(editValue)
    	return (editLayout, editValue)
    
    def toCSV(self, csvWriter):
        csvWriter.writerow([self.name, self.label, self.font, str(self.position), str(self.show)])
    
    def writeXML(self, xmlWriter):
        xmlWriter.writeStartElement('field')
        xmlWriter.writeAttribute('name', self.name)
        xmlWriter.writeTextElement('label', self.label)
        xmlWriter.writeTextElement('font', self.font)
        xmlWriter.writeTextElement('position', str(self.position))
        xmlWriter.writeTextElement('show', str(self.show))
        xmlWriter.writeEndElement()
    
    def copy(self):
        return Field(self.vocab, self.name, self.label, self.font, self.position, self.show)

class FieldInstance:
    def __init__(self, field, value, parent = None):
    	self.field = field
    	self.value = value
    	
    	self.layout = QtGui.QHBoxLayout()
    	
    	if self.field.name != 'english' and self.field.name != 'foreign':
    	    self.labelLabel = QtGui.QLabel(field.label + ':')
    	    self.labelLabel.setAlignment(QtCore.Qt.AlignRight)
    	    self.layout.addWidget(self.labelLabel)
    	    
    	self.valueLabel = QtGui.QLabel(self.value)
    	self.valueLabel.setFont(field.vocab.fonts[field.font])
    	if self.field.name != 'english' and self.field.name != 'foreign':
    	    self.valueLabel.setAlignment(QtCore.Qt.AlignLeft)
    	else:
    	    self.valueLabel.setAlignment(QtCore.Qt.AlignCenter)
    	self.layout.addWidget(self.valueLabel)
    
    def setVisible(self, visible):
        # don't show this field if it's not being used
        if (self.value == ''):
            visible = False
    	if self.field.name != 'english' and self.field.name != 'foreign':
    	    self.labelLabel.setVisible(visible)
    	self.valueLabel.setVisible(visible)
    
    def setValue(self, newValue):
        self.value = newValue
        self.valueLabel.setText(newValue)
    
    def refreshFont(self):
    	self.valueLabel.setFont(self.field.vocab.fonts[self.field.font])
        
class FieldMetaEdit(QtCore.QObject):
    def __init__(self, field, grid, parent = None):
        QtCore.QObject.__init__(self, parent)
        self.field = field
        
        self.deleteMe = False
        
        self.labelEdit = QtGui.QLineEdit(self.field.label)
        self.deleteButton = QtGui.QPushButton('delete') #FIXME put a 'trash can' icon on this button
        if self.field.name == 'chapter' or self.field.name == 'difficulty' or self.field.name == 'english' or self.field.name == 'foreign':
            # some fields should be undeletable
            self.deleteButton.setEnabled(False)
        self.selectPosition = QtGui.QComboBox()
        for position in fieldPositionLabel:
            self.selectPosition.addItem(position)
            if fieldPositionLabel[position] == self.field.position:
                self.selectPosition.setCurrentIndex(self.selectPosition.count()-1)
        self.selectShow = QtGui.QComboBox()
        for show in fieldShowLabel:
            self.selectShow.addItem(show)
            if fieldShowLabel[show] == self.field.show:
                self.selectShow.setCurrentIndex(self.selectShow.count()-1)
        self.selectFont = QtGui.QComboBox()
        for font in self.field.vocab.fonts:
            self.selectFont.addItem(font)
            if font == self.field.font:
                self.selectFont.setCurrentIndex(self.selectFont.count()-1)
        
        row = grid.rowCount()
        grid.addWidget(self.deleteButton, row, 0)
        grid.addWidget(QtGui.QLabel(self.field.name), row, 1)
        grid.addWidget(self.labelEdit, row, 2)
        grid.addWidget(self.selectPosition, row, 3)
        grid.addWidget(self.selectShow, row, 4)
        grid.addWidget(self.selectFont, row, 5)
        
        self.connect(self.deleteButton, QtCore.SIGNAL('clicked()'), self.delete)
    
    def delete(self):
        if self.deleteMe:
            self.deleteMe = False
            self.deleteButton.setText('delete')
            self.labelEdit.setEnabled(True)
            self.selectPosition.setEnabled(True)
            self.selectShow.setEnabled(True)
            self.selectFont.setEnabled(True)
        else:
            self.deleteMe = True
            self.deleteButton.setText('undelete')
            self.labelEdit.setEnabled(False)
            self.selectPosition.setEnabled(False)
            self.selectShow.setEnabled(False)
            self.selectFont.setEnabled(False)

class FieldPosition:
    unknown = 0
    # these are prefix and postfix onto the foreign language word
    PREFIX = 1
    POSTFIX = 2
    #FIXME should I allow prefix and postfix on the home language word?
 #   TOP = 3
 #   BOTTOM = 4
    LEFT = 5
    RIGHT = 6
    CENTRE = 7

fieldPositionLabel = {}
fieldPositionLabel['Prefix'] = FieldPosition.PREFIX
fieldPositionLabel['Postfix'] = FieldPosition.POSTFIX
fieldPositionLabel['Top-Left'] = FieldPosition.LEFT
fieldPositionLabel['Top-Right'] = FieldPosition.RIGHT
fieldPositionLabel['Centre'] = FieldPosition.CENTRE

fieldAlignment = {}
fieldAlignment[FieldPosition.LEFT] = QtCore.Qt.AlignLeft
fieldAlignment[FieldPosition.RIGHT] = QtCore.Qt.AlignRight
fieldAlignment[FieldPosition.CENTRE] = QtCore.Qt.AlignCenter

class FieldShow:
    ALWAYS = 0
    ONHINT = 1
    ONREVEAL = 2
    NEVER = 3

fieldShowLabel = {}
fieldShowLabel['Always'] = FieldShow.ALWAYS
fieldShowLabel['On Hint'] = FieldShow.ONHINT
fieldShowLabel['On Reveal'] = FieldShow.ONREVEAL
fieldShowLabel['Never'] = FieldShow.NEVER
